package com.threegroup.admin.security.service;

import com.threegroup.admin.dao.SysMenuDao;
import com.threegroup.admin.entity.SysRoleEntity;
import com.threegroup.admin.entity.SysUserEntity;
import com.threegroup.admin.enums.SuperAdminEnum;
import com.threegroup.admin.security.exception.UserRoleNotFoundException;
import com.threegroup.admin.security.user.SecurityUserFactory;
import com.threegroup.admin.security.user.SysUser;
import com.threegroup.admin.service.IAdminService;
import com.threegroup.admin.service.ISysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 将SpringSecurity 中的用户管理和数据库的管理员对应起来
 */
@Service
public class SecurityUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private IAdminService adminService;

    @Resource
    private ISysRoleService roleService;

    @Resource
    private SysMenuDao sysMenuDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUserEntity userEntity = adminService.selectUserByUsername(username);

        if (userEntity == null) {
            throw new UsernameNotFoundException(String.format("[%s]该账号未注册,无法登录", username));
        }

        // 查询出角色信息封装到admin中
        SysRoleEntity roleEntity = roleService.selectByUserId(userEntity.getId());
        if (roleEntity == null) {
            throw new UserRoleNotFoundException(String.format("[%s]该账号未配置角色,无法登录", username));
        }

        List<String> roleNames = new ArrayList<>();
        roleNames.add(roleEntity.getName());
        userEntity.setRoleNames(roleNames);
        userEntity.setRole(roleEntity);

        // 权限信息 系统管理员拥有最高权限
        List<String> permissionsList;
        if (userEntity.getSuperAdmin() == SuperAdminEnum.YES.value()) {
            permissionsList = sysMenuDao.getPermissionsList();
        } else {
            permissionsList = sysMenuDao.getUserPermissionsList(userEntity.getId());
        }

        // 用户权限列表
        Set<String> permsSet = new HashSet<>();
        for(String permissions : permissionsList){
            if(StringUtils.isBlank(permissions)){
                continue;
            }
            permsSet.addAll(Arrays.asList(permissions.trim().split(",")));
        }

        userEntity.setPermissionList(new ArrayList<>(permsSet));

//        return new SysUser(username, userEntity.getPassword(), mapToGrantedAuthorities(new ArrayList<>(permsSet)), userEntity);
        return SecurityUserFactory.create(userEntity);
    }

    private List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}
